import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12

Window {
    id: root

    // public ==============================================================================

    property var modelChannel: ["FAM", "VIC", "ROX", "CY5"]
    property var modelSample: ["Sample1", "Sample2", "Sample3", "Sample4"]
    property var modelType: ["标准", "未知", "阴性"]
    property var colorList: [{}, {}, {}]        // ColorList[0]: 按通道
                                                // ColorList[1]: 按样本名 Map key=sampleName

    signal accept(var colorList, var mode)
    signal reject()

    // private =============================================================================
    property var currentColor: "white"

    property var defaultColorList: [Qt.rgba(66/255,  46/255,  205/255, 1),
                                      Qt.rgba(0/255,   196/255, 115/255, 1),
                                      Qt.rgba(255/255, 135/255, 62/255, 1),
                                      Qt.rgba(255/255, 43/255,  55/255, 1),
                                      Qt.rgba(77/255, 43/255,  255/255, 1),
                                      Qt.rgba(255/255, 233/255,  99/255, 1),
                                      Qt.rgba(87/255, 233/255,  55/255, 1),
                                      Qt.rgba(34/255, 234/255,  144/255, 1),]

    onVisibleChanged: {
        if(visible){
            boxColorPicker.modelSample = CAnalysis.getList_name()
            boxColorPicker.modelChannel = CAnalysis.getList_channel()
        }
    }


    onModelChannelChanged: {
        combox_configItem.changeCurrentIndex(0)
        combox_configType.changeCurrentIndex(0)
        combox_configItem.model = modelChannel
        initDefaultColor()
    }onModelSampleChanged: {
        combox_configItem.changeCurrentIndex(0)
        combox_configType.changeCurrentIndex(0)
        combox_configItem.model = modelChannel
        initDefaultColor()
    }

    function initDefaultColor(){    // 恢复默认颜色
        for(let i = 0; i < modelChannel.length; i ++){
            colorList[0][modelChannel[i]] = defaultColorList[i]
        }
        for(let i = 0; i < modelSample.length; i ++){
            colorList[1][modelSample[i]] = defaultColorList[i]
        }
        combox_configType.changeCurrentIndex(0)
        combox_configItem.changeCurrentIndex(0)
        currentColor = colorList[0][modelChannel[0]]
        colorPicker.setColor(currentColor)
    }


    Component.onCompleted: {
        initDefaultColor()
    }

    function onConfigTypeChanged(){            // 配置类型修改
        if(combox_configType.getText()==="按通道"){
            combox_configItem.model = modelChannel
        }if(combox_configType.getText()==="按样本名称"){
            combox_configItem.model = modelSample
        }
        onConfigItemChanged()
    }
    function onConfigItemChanged(){             // 配置
        currentColor = colorList[combox_configType.getIndex()][combox_configItem.getText()]
        colorPicker.setColor(currentColor)
    }


    // 样式 ===================================================================================
    title: "颜色配置"
    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint


    minimumHeight: 460*dp
    maximumHeight: 460*dp
    minimumWidth: 400*dp
    maximumWidth: 400*dp

    Column{
        y: 40*dp;  x: 40*dp
        width: root.width - 80*dp
        spacing: 20*dp
        ColorPicker{
            id: colorPicker
            width: parent.width
            onColorChanged: {
                currentColor = c
                colorList[combox_configType.getIndex()][combox_configItem.getText()] = c
            }
        }
        CustomComboBox{
            id: combox_configType
            editWidth: parent.width-textWidth
            title: "配置类型"
//            model: ["按通道", "按样本名称", "按类型"]
            model: ["按通道", "按样本名称"]
            onCurrentText: {
                onConfigTypeChanged()
            }
        }
        Item{
            height: 40*dp
            width: parent.width
            CustomComboBox{
                id: combox_configItem
                model: ["FAM", "VIC", "ROX", "CY5"]
                onCurrentText: {
                    onConfigItemChanged()
                }
            }Rectangle{
                height: 40*dp;   width: 80*dp
                x: parent.width-80*dp
                border.color: "#CCCCCC"
                border.width: 2
                color: currentColor
                radius: 3
            }
        }

    }
    Item{
        y: root.height-90*dp
        width: root.width
        PushButton{
            title: "取消"
            defaultMode2: true
            x: root.width/2 - 130*dp
            onReleased: {
                root.reject()
                root.close()
            }
        }PushButton{
            title: "确定"
            defaultMode1: true
            x: root.width/2 + 10*dp
            onReleased: {
                root.accept(colorList[combox_configType.getIndex()], combox_configType.getIndex())
                root.close()
            }
        }
    }
}
